package MathTest.Question;

import java.util.Stack;

public class Random {
    public Random(){}
    public char RandomChar(int stage){
        int num = (int)(Math.random()*stage);
        switch (num){
            case 0:return'+';
            case 1:return'-';
            case 2:return'*';
            case 3:return'÷';
            default: return'o';
        }
    }
    public int  RandomNum(int e){
        int num =(int)(Math.random()*e);
        return num;
    }
    public String RandomBranket(String s,int choice){
        String t = "";
        switch (RandomNum(2)) {      //随机产生括号
            case 0:                    //不带括号
                t = s;
                break;
            case 1:                     //带括号
                if (choice > 1) {
                    String []str = null;
                    str = s.split(" ");   //字符串转化为字符串数组
                    Stack<String> Z = new Stack<String>();
                    String temp = new String();
                    for (int k=0; k!=str.length-2; k++) {                 //遍历前length-2个元素
                        if ((str[k].equals("+") || str[k].equals("-"))) {            //如果是加减
                            int b = k+2;
                            int a = k+1;
                            if (str[b].equals("*") || str[b].equals("÷")) {             //下一个符号是乘除
                                temp = Z.pop();
                                Z.push("(");
                                Z.push(temp);
                                Z.push(str[k]);
                                Z.push(str[a]);
                                Z.push(")");
                                k++;
                            } else                                                     //下一个符号是加减
                                Z.push(str[k]);
                        }
                        else                          //如果是乘除数字就直接入栈
                            Z.push(str[k]);
                    }
                    for(int m=str.length-2;m<str.length;m++)
                        Z.push(str[m]);
                    String[] ZZ = new String[Z.size()];
                    int p = Z.size() - 1;
                    while (Z.size() != 0) {        //将栈中的元素放到数组中
                        temp = Z.pop();
                        ZZ[p] = temp;
                        p--;
                    }
                    t = "";
                    for (int q = 0; q < ZZ.length; q++)
                        t = t + ZZ[q] + " ";
                    ZZ = null;
                    break;
                }
                else{
                    t = s;
                    break;
                }
        }
        return t;
    }
}
